from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe

from rbac.models import Menu, Permission

for item in settings.ICON_LIST:
    item[1] = mark_safe(item[1])


class BootstrapModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(BootstrapModelForm, self).__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = field.label


class MenuModelForm(forms.ModelForm):
    class Meta:
        model = Menu
        fields = ['title', 'icon']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'icon': forms.RadioSelect(
                choices=settings.ICON_LIST,
                attrs={'class': 'clearfix'}
            )
        }


class SecondMenuModelForm(BootstrapModelForm):
    class Meta:
        model = Permission
        exclude = ['pid']


class PermissionModelForm(BootstrapModelForm):
    class Meta:
        model = Permission
        fields = ['title', 'name', 'url']


# class MultiAddPermissionForm(forms.Form):
#     title = forms.CharField(
#         widget=forms.TextInput(attrs={'class': 'form-control'})
#     )
#     url = forms.CharField(
#         widget=forms.TextInput(attrs={'class': 'form-control'})
#     )
#     name = forms.CharField(
#         widget=forms.TextInput(attrs={'class': 'form-control'})
#     )
#     menu = forms.ChoiceField(
#         choices=[(None, '-------')],
#         widget=forms.Select(attrs={'class': 'form-control'}),
#         required=False,
#     )
#     pid = forms.ChoiceField(
#         choices=[(None, '--------')],
#         widget=forms.Select(attrs={'class': 'form-control'}),
#         required=False,
#     )
#
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.fields['menu'].choices += Menu.objects.all().values_list('id', 'title')
#         self.fields['pid'].choices += Permission.objects.filter(pid__isnull=True).exclude(
#             menu__isnull=True).values_list('id', 'title')

class MultiAddPermissionForm(forms.Form):
    title = forms.CharField(label='权限名称', widget=forms.TextInput(attrs={'class': 'form-control'}))
    name = forms.CharField(label='权限别名', widget=forms.TextInput(attrs={'class': 'form-control'}))
    url = forms.CharField(label='带正则的URL', widget=forms.TextInput(attrs={'class': 'form-control'}))
    pid_id = forms.ChoiceField(
        label='父级权限',
        choices=[(None, '--------')],
        widget=forms.Select(attrs={'class': 'form-control'}),
        required=False
    )
    menu_id = forms.ChoiceField(
        label='菜单名称',
        choices=[(None, '--------')],
        widget=forms.Select(attrs={'class': 'form-control'}),
        required=False
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['pid_id'].choices += Permission.objects.filter(pid__isnull=True).exclude(
            menu__isnull=False).values_list('id', 'title')
        self.fields['menu_id'].choices += Menu.objects.values_list('id', 'title')


class MultiEditPermissionForm(forms.Form):
    id = forms.IntegerField(
        widget=forms.HiddenInput()
    )
    title = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    url = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    name = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    menu_id = forms.ChoiceField(
        choices=[(None, '-------')],
        widget=forms.Select(attrs={'class': 'form-control'}),
        required=False,
    )
    pid_id = forms.ChoiceField(
        choices=[(None, '--------')],
        widget=forms.Select(attrs={'class': 'form-control'}),
        required=False,
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['menu_id'].choices += Menu.objects.all().values_list('id', 'title')
        self.fields['pid_id'].choices += Permission.objects.filter(pid__isnull=True). \
            exclude(menu__isnull=True).values_list('id', 'title')

